package com.costeaalex.MIDlets.remote.screens;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.rms.RecordStore;

import com.costeaalex.MIDlets.remote.BlueMIDlet;

public class ScreenStartupSettings extends Thread implements CommandListener
	{
	
	private Command cmExit;
	private Command cmSetup;
	private BlueMIDlet midlet;
	private StringItem bodyText;
	private RecordStore rsL;
	private boolean setup=false;
	
	public Form form;
	
	public ScreenStartupSettings(RecordStore rs, BlueMIDlet midl)
		{
		midlet=midl;
		
		rsL=rs;
		
		form=new Form("Startup");
		bodyText=new StringItem("", "Select setup to delete the current settings and restart.");
		form.append(bodyText);
				
		cmExit=new Command("Exit", Command.EXIT, 1);
		cmSetup=new Command("Setup", Command.SCREEN, 2);
		
		form.addCommand(cmExit);
		form.addCommand(cmSetup);
		form.setCommandListener(this);
		
		}
	
	public void run()
		{
		try
			{
			sleep(2000);
			}
		catch (InterruptedException e)
			{
			e.printStackTrace();
			}
		
		if(!setup)
			{
			form.deleteAll();
			bodyText=new StringItem("", "Please wait. Application is initializing...");
			form.append(bodyText);
			midlet.startUp();
			}
		}
	
	public void commandAction(Command c, Displayable d)
		{
		
		if(c==cmExit)
			{
			midlet.shutDown();
			}
				
		if(c==cmSetup)
			{
			try
				{
				setup=true;
				rsL.closeRecordStore();
				RecordStore.deleteRecordStore("SETTINGS");
				midlet.shutDown();
				}
			catch (Exception e)
				{
				e.printStackTrace();
				}
			}
		}

	}
